#include "distance.h"

long duration;
double lastDistance = 0;
double distance = 0; //cm
double measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH,4117); //最远70厘米
  // Calculating the distance
  lastDistance = distance;
  distance = duration * 0.34 / 2; // Speed of sound wave divided by 2 (go and back)
  return distance; //mm
}
